package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ItemLongTaskProgressBinding;
import com.jwdf.uda.event.LongTaskEvent;
import com.jwdf.uda.ui.fragment.progress.TaskList;
import com.jwdf.uda.utils.EventBusUtil;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 */
public class LongTaskListAdapter extends BaseAdapter<TaskList>{
    private static final int ITEM_TEXT = 0;
    private static final int ITEM_IMAGE = 1;
    private List<TaskList> mItems = new ArrayList<>();
    private Context mContext;

    public LongTaskListAdapter(Context context) {
        super(context);
        mContext = context;
    }

    public void setDetailData(List<TaskList> taskLists) {
        mItems.clear();
        for(int i=0;i<taskLists.size();i++) {
            addPlanImageItem(taskLists.get(i),i);
        }
//        addPlanImageItem(taskLists);
        append(mItems);
        notifyDataSetChanged();
    }

    private void addPlanImageItem(TaskList taskLists ,int location) {
        TaskList item = new TaskList();
        item.setName(taskLists.getName());
        item.setType(taskLists.getType());
        item.setmTaskType(taskLists.getmTaskType());
        item.setLocation(location);
        mItems.add(item);

    }

    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new VotePlanTextViewHolder(mInflater.inflate(R.layout.item_long_task_progress, parent, false));
    }

    public class VotePlanTextViewHolder extends BaseViewHolder<TaskList>{
        public ItemLongTaskProgressBinding mItemBinding;
        private int eventType;
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(eventType == item.getmTaskType()) {
                    mItemBinding.flikerbar.setProgress(msg.arg1);
                    if(msg.arg1 == 100) {
                        //                    mItemBinding.flikerbar.finishLoad();
                        EventBusUtil.post(new LongTaskEvent(LongTaskEvent.LONG_TASK_QUESTION_CLOSE, item.getType()));
                    }
                }
            }
        };

        public VotePlanTextViewHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
            EventBusUtil.registerEvent(this);
        }
        @Override
        protected void onBindView(TaskList item) {
            mItemBinding.tvName.setText(item.getName());
            mItemBinding.tvType.setText(item.getType());
            mItemBinding.flikerbar.setProgress(0);
//            mItemBinding.flikerbar.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(!mItemBinding.flikerbar.isFinish()){
//                        mItemBinding.flikerbar.toggle();
//                    }
//                }
//            });

            downLoad();

        }

//        public void reLoad(View view) {
//            if(mItemBinding.flikerbar.isFinish()){
//                // 重新加载
//                mItemBinding.flikerbar.reset();
//                downLoad();
//            }
//        }
        private void downLoad() {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    while(true){
//                        try {
//                            float progress = mItemBinding.flikerbar.getProgress();
//                            progress  += 2;
//                            Thread.sleep(200);
//                            Message message = handler.obtainMessage();
//                            message.arg1 = (int) progress;
//                            handler.sendMessage(message);
//                            if(progress == 100){
//                                break;
//                            }
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }).start();
        }

       public void  onEvent(LongTaskEvent event){
            switch (event.getEvent()){
                case LongTaskEvent.LONG_TASK_QUESTION_END:
                    eventType=event.getType();
                    if(event.getType()==item.getmTaskType()) {
                        Message message = handler.obtainMessage();
                        message.arg1 = (int) event.getProgress();
                        Log.i("-----111-----"+item.getmTaskType(), message.arg1 + "--"+event.getProgress());
                        handler.sendMessage(message);
                    }
                    break;
            }
        }

    }

}
